package com.work.music.dao;

import com.work.music.entity.MusicLyricsManager;
import com.work.music.entity.MusicUser;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface MusicUserMapper {
    @Delete({
        "delete from music_user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into music_user (user_name, user_pwd, ",
        "email, phone, headimg, ",
        "nick_name, user_desc, ",
        "register_time, register_ip, ",
        "register_type, isdisabled, ",
        "state)",
        "values (#{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR}, ",
        "#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{headimg,jdbcType=VARCHAR}, ",
        "#{nickName,jdbcType=VARCHAR}, #{userDesc,jdbcType=VARCHAR}, ",
        "#{registerTime,jdbcType=TIMESTAMP}, #{registerIp,jdbcType=VARCHAR}, ",
        "#{registerType,jdbcType=INTEGER}, #{isdisabled,jdbcType=INTEGER}, ",
        "#{state,jdbcType=INTEGER})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(MusicUser record);

    @Select({
        "select",
        "id, user_name, user_pwd, email, phone, headimg, nick_name, user_desc, register_time, ",
        "register_ip, register_type, isdisabled, state",
        "from music_user",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_pwd", property="userPwd", jdbcType=JdbcType.VARCHAR),
        @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
        @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
        @Result(column="headimg", property="headimg", jdbcType=JdbcType.VARCHAR),
        @Result(column="nick_name", property="nickName", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_desc", property="userDesc", jdbcType=JdbcType.VARCHAR),
        @Result(column="register_time", property="registerTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="register_ip", property="registerIp", jdbcType=JdbcType.VARCHAR),
        @Result(column="register_type", property="registerType", jdbcType=JdbcType.INTEGER),
        @Result(column="isdisabled", property="isdisabled", jdbcType=JdbcType.INTEGER),
        @Result(column="state", property="state", jdbcType=JdbcType.INTEGER)
    })
    MusicUser selectByPrimaryKey(Long id);

    @Select({
        "select",
        "id, user_name, user_pwd, email, phone, headimg, nick_name, user_desc, register_time, ",
        "register_ip, register_type, isdisabled, state",
        "from music_user"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_pwd", property="userPwd", jdbcType=JdbcType.VARCHAR),
        @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
        @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
        @Result(column="headimg", property="headimg", jdbcType=JdbcType.VARCHAR),
        @Result(column="nick_name", property="nickName", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_desc", property="userDesc", jdbcType=JdbcType.VARCHAR),
        @Result(column="register_time", property="registerTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="register_ip", property="registerIp", jdbcType=JdbcType.VARCHAR),
        @Result(column="register_type", property="registerType", jdbcType=JdbcType.INTEGER),
        @Result(column="isdisabled", property="isdisabled", jdbcType=JdbcType.INTEGER),
        @Result(column="state", property="state", jdbcType=JdbcType.INTEGER)
    })
    List<MusicUser> selectAll();

    @Update({
        "update music_user",
        "set user_name = #{userName,jdbcType=VARCHAR},",
          "user_pwd = #{userPwd,jdbcType=VARCHAR},",
          "email = #{email,jdbcType=VARCHAR},",
          "phone = #{phone,jdbcType=VARCHAR},",
          "headimg = #{headimg,jdbcType=VARCHAR},",
          "nick_name = #{nickName,jdbcType=VARCHAR},",
          "user_desc = #{userDesc,jdbcType=VARCHAR},",
          "register_time = #{registerTime,jdbcType=TIMESTAMP},",
          "register_ip = #{registerIp,jdbcType=VARCHAR},",
          "register_type = #{registerType,jdbcType=INTEGER},",
          "isdisabled = #{isdisabled,jdbcType=INTEGER},",
          "state = #{state,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(MusicUser record);



    /**
     * 根据手机号或邮箱地址获取用户个人信息
     * @param phone
     * @return
     */
    @Select({
            "select",
            "id, user_name, user_pwd, email, phone, headimg, nick_name, user_desc, register_time, ",
            "register_ip, register_type, isdisabled, state",
            "from music_user",
            "where phone = #{phone,jdbcType=VARCHAR} or email = #{phone,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_pwd", property="userPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="headimg", property="headimg", jdbcType=JdbcType.VARCHAR),
            @Result(column="nick_name", property="nickName", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_desc", property="userDesc", jdbcType=JdbcType.VARCHAR),
            @Result(column="register_time", property="registerTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="register_ip", property="registerIp", jdbcType=JdbcType.VARCHAR),
            @Result(column="register_type", property="registerType", jdbcType=JdbcType.INTEGER),
            @Result(column="isdisabled", property="isdisabled", jdbcType=JdbcType.INTEGER),
            @Result(column="state", property="state", jdbcType=JdbcType.INTEGER)
    })
    MusicUser selectByPhoneOrEmail(String phone);

    /**
     * 获取总条数
     * @param msg
     * @return
     */
    @Select("select ifnull(count(*), 0) from music_user ${msg}")
    int selectLyricsCount(@Param("msg") String msg);

    /**
     * 获取用户列表信息
     * @param msg
     * @param start
     * @param count
     * @return
     */
    @Select({
            "select",
            "id, user_name, user_pwd, email, phone, headimg, nick_name, user_desc, register_time, ",
            "register_ip, register_type, isdisabled, state",
            "from music_user",
            "${msg} limit #{start},#{count}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_pwd", property="userPwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="email", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="phone", property="phone", jdbcType=JdbcType.VARCHAR),
            @Result(column="headimg", property="headimg", jdbcType=JdbcType.VARCHAR),
            @Result(column="nick_name", property="nickName", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_desc", property="userDesc", jdbcType=JdbcType.VARCHAR),
            @Result(column="register_time", property="registerTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="register_ip", property="registerIp", jdbcType=JdbcType.VARCHAR),
            @Result(column="register_type", property="registerType", jdbcType=JdbcType.INTEGER),
            @Result(column="isdisabled", property="isdisabled", jdbcType=JdbcType.INTEGER),
            @Result(column="state", property="state", jdbcType=JdbcType.INTEGER)
    })
    List<MusicUser> selectLyricsList(@Param("msg") String msg, @Param("start") int start, @Param("count") int count);
}